﻿
using CardMonitor.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Zzj.Model;
using Zzj.Windows;

namespace Zzj.Pages
{
    /// <summary>
    /// Print.xaml 的交互逻辑
    /// </summary>
    public partial class PrintPage : Page
    {
        MainWindow mainWindow = null;
        SynchronizationContext _syncContext = null;
        List<BillModel> items = new List<BillModel>();
        int m_iInit1 = -1;
        //int m_iStatus1 = -1;
        int m_iStatus = -1;
        public PrintPage()
        {
            InitializeComponent();
        }
        
        public PrintPage(List<BillModel> bills,MainWindow _win)
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;

            mainWindow = _win;
            
            items = bills;
            listView.ItemsSource = items;
        }
        public void loadData()
        {
            items.Add(new BillModel());
            items.Add(new BillModel());
            items.Add(new BillModel());
            items.Add(new BillModel());
        }
        private void goIndex(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).GoBack();
        }

      

        

        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }

        private void Button_Click_PrintWeightTicket(object sender, RoutedEventArgs e)//磅单打印
        {

            var btn = sender as Button;
            var item = btn.DataContext as BillModel;
            string PrinterStatusPrompt = App.getConfig().PrinterStatusPrompt;
            if (PrinterStatusPrompt.Equals("1"))
            {
             
                MsPrinterUtils.SetUsbportauto();//自动配置USB端口
                m_iInit1 = MsPrinterUtils.SetInit();//初始化打印机
                m_iStatus = MsPrinterUtils.GetStatus();//获取打印机状态
                if (m_iStatus == 7)
                {
                    showMessage("打印机缺纸,请联系工作人员");
                }

                else if (m_iStatus == 0 || m_iStatus == 8)
                {
                    
                }
                else
                {
                    showMessage("打印机异常" + m_iStatus);
                }


            }
            else
            {
  
                    PrintWindow printWindow = new PrintWindow();
                    printWindow.setBillMode(item);

                    if ("0".Equals(item.Type) || "5".Equals(item.Type) || "4".Equals(item.Type))
                    {
                        printWindow.setTemplete("tplWeighsale", "1");
                    }
                    else
                    {
                        printWindow.setTemplete("tplWeighPurchase", "1");
                    }
                    printWindow.Owner = mainWindow;
                    bool result = (bool)printWindow.ShowDialog();
                    if (!result)
                    {
                        showMessage("打印失败，请联系工作人员");
                    }
                    else
                    {
                        Index index = new Index(mainWindow);

                        NavigationService.GetNavigationService(this).Navigate(index);
                    }
                }

            }






        

        private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
